mini.parse();
var grid = mini.get("ReportAccessStorageGrid");
grid.setSortField("UPDATE_TIME,BRANCH_ORG_ID");
grid.setSortOrder("desc");
grid.load();
var projectName = $("#projectName").val();

function editReportAccessStorage(row) {
//	if(row){
//		openWindowsReportAccessStorage(row.id,"edit","编辑");
//	 }else{
//		 row = grid.getSelected();
//		if(row){
//			openWindowsReportAccessStorage(row.id,"edit","编辑");
//		}else{
//			  mini.alert("请选中一条记录");
//		}
//	} 	
	 var rows = grid.getSelecteds();
	 var l = rows.length;
	 if(rows && l == 1){
		 openWindowsReportAccessStorage(rows[0].id,"edit","编辑");
	 }else if(rows && l > 1){
		 mini.alert("只能选中一条记录");
	 }else{
		 mini.alert("请选中一条记录");
	 } 
}
function openWindowsReportAccessStorage(id,openType,WindowName){
	mini.open({
        url: projectName+"/ReportAccessStorageAdmin/EditPage",
        title: WindowName, width: 700, height: 500,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = { action: openType, id: id };
            iframe.contentWindow.CheckEventReportAccessStorage(data);
        },
        ondestroy: function (action) {
            grid.reload();
        }
    });
}
function addReportAccessStorage(){
	openWindowsReportAccessStorage("","new","新增");
}

function onrowdblclick(e){
	 var record = e.record;
	openWindowsReportAccessStorage(record.id,"look","查看");
}


function seachRow(){
	 var form = new mini.Form("#formseach");
    var data = form.getData(true, false);
    grid.load(data);
	
}
function resetForm(){
	 var form = new mini.Form("#formseach");
     form.reset();
}
function exportReportAccessStorage() {
	var rows = grid.getSelecteds();
	var idString = '';
	var url = projectName +"/ReportAccessStorageAdmin/exportReportAccessStorageExcel?";
    if (rows.length > 0) {
	    var ids = [];
	    for (var i = 0, l = rows.length; i < l; i++) {
	        var r = rows[i];
	        ids.push(r.id);
	    }
	    idString = ids.join(',');    
	    mini.get("ids").setValue(idString);
	    util.exportDataToClient($('#ReportAccessStorageListForm'), url);
	} else {
		
		mini.confirm('导出全部？如查询条件不为空，则导出符合查询条件的所有数据.','确定？',function(action) {
			if (action == 'ok') {
				mini.get('ids').setValue('');
				util.exportDataToClient($('#ReportAccessStorageListForm'), url);
			} else {
				window.close();
			}
		});
	}
}	

function removeReportAccessStorage(){
	var rows = grid.getSelecteds();
    if (rows.length > 0) {
    	mini.confirm('确定删除选中记录？','确定？',function(action) {
    		if (action == 'ok') {
                var ids = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.id);
                }
                var idString = ids.join(',');
                
                $.ajax({
                    url: projectName+"/ReportAccessStorageAdmin/batchDelReportAccessStorage?ids=" +idString,
                    type : 'post',
                    dataType : 'json',
                    success: function (data, textStatus, jqXHR) {
                    	if (data.status == '0' || data.status == '1') {
                    		mini.alert(data.message, '', function() {
    							grid.reload();
    						});
    					} else {
    						mini.alert("发生错误!", '', function() {
    							grid.reload();
    						});
    					}
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                    	mini.alert("发生错误，操作失败！");
                    }
                });
             
    		} else {
    			window.close();
    		}
    	});
    	
    } else {
        mini.alert("请选中一条记录");
    }

}

function importReportAccessStorage(){
	mini.open({
        url: projectName+"/AttachmentAdmin/importExcelPage?configName=ReportAccessStorageEntityExcel&importType=add",
        title: "数据导入", width: 700, height: 500,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = { action: openType, id: id };
            iframe.contentWindow.CheckEventReportAccessStorage(data);
        },
        ondestroy: function (action) {
            grid.reload();
        }
    });
}
/**查看详情*/
function viewReportAccessStorageDetail(){
	
}
/**查看条码*/
function viewReportAccessStorageBarcode(){
	 var rows = grid.getSelecteds();
	 var l = rows.length;
	 if(l==0){
		 mini.alert("请选择一条记录");
		 return;
	 }else if(l>1){
		 mini.alert("只能选择一条记录");
		 return;
	 }
	 var row = rows[0];
	mini.open({
        url: projectName+"/ReportAccessStorageBarcodeAdmin/ListPage",
        title: "查看条码", width: 700, height: 500,
        onload: function () {
            var iframe = this.getIFrameEl();
            iframe.contentWindow.setDefaultParam(row);
        },
        ondestroy: function (action) {
        	
        }
    });
}


/**分公司改变*/
function onChangeBranch(e){
	var orgId = e.value;
	var office = mini.get("officeOrgId");
	var url = projectName+"/OrgAdmin/queryList?orgType=2&parentOrgId="+orgId;
	office.setUrl(url);
}

function onSelectStorage(e){
	var orgId = mini.get("officeOrgId").getValue();
	if(!orgId){
		orgId = mini.get("branchOrgId").getValue();
	}
	var physicalStorageId = mini.get("storagePhyId").getValue();
	if(!physicalStorageId){
		mini.alert("请先选择仓库地点");
		return;
	}
    var btnEdit = this;
    mini.open({
        url: projectName+"/StStorageAdmin/stStorageChoose",
        title:"仓库选择", width: 780, height: 500,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = {physicalStorageId: physicalStorageId[0],orgId:orgId,chooseType:"true"};
            iframe.contentWindow.setData(data);
        },
        ondestroy: function (action) {
        	if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                data = mini.clone(data);
                var rows = data.rows;
                var ids = [], texts = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var row = rows[i];
                    ids.push(row.storageId);
                    texts.push(row.storageName);
                }
                btnEdit.setValue(ids);
                btnEdit.setText(texts);
            }
        }
    });
}

function onSelectStoragePhy(e){
	var orgId = mini.get("officeOrgId").getValue();
	if(!orgId){
		orgId = mini.get("branchOrgId").getValue();
	}
    var btnEdit = this;
    var v = btnEdit.getValue();
    mini.open({
        url: projectName+"/PhyStorageAdmin/phyStorageChoose",
        title:"仓库地点选择", width: 780, height: 500,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = {orgId: orgId,chooseType:"true"};
            iframe.contentWindow.setData(data);
        },
        ondestroy: function (action) {
        	if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                data = mini.clone(data);
                var rows = data.rows;
                var ids = [], texts = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var row = rows[i];
                    ids.push(row.physicalStorageId);
                    texts.push(row.physicalStorageName);
                }
                btnEdit.setValue(ids);
                btnEdit.setText(texts);
                if(btnEdit.getValue()!=v){
					mini.get("storageId").setValue("");
					mini.get("storageId").setText("");
				}
            }
        }
    });
}